package com.sirdc.modules.sys.web;

import com.sirdc.modules.core.exceptions.SystemException;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.sys.entity.SysUserLogin;
import com.sirdc.modules.sys.filter.SysUserLoginFilter;
import com.sirdc.modules.sys.service.SysUserLoginService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/userLogin")
public class SysUserLoginController extends JsonBaseController {
    @Autowired
    private SysUserLoginService sysUserLoginService;

    @Override
    protected String getView(String view) {
        return "/modules/userLogin/" + view;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage(Model model) {
        return getView("query");
    }

    /**
     * 获取筛选数据
     * @author: huiyang.yu
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute SysUserLoginFilter filter) {
        List<SysUserLogin> demoStudents = sysUserLoginService.queryUserOnlineList(filter);
        return coverJqGrid(filter, demoStudents);
    }


    /**
     * 导出通讯录
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportOnLineCount", method = RequestMethod.GET)
    public void exportMailList(HttpServletResponse reponse, HttpServletRequest request) {
        HSSFWorkbook wb = sysUserLoginService.queryUserOnlineCount();
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String nowDate = df.format(new Date());
            String filename = "用户登录次数"+nowDate+".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
